package com.woniu.common.utils.token;

import com.woniu.common.constant.Constants;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.enums.SysType;
import com.woniu.common.exception.CustomException;
import com.woniu.common.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class TokenUtils {

    public static AccToken getToken(HttpServletRequest request, SysType sysType) {
        String header = request.getHeader(Constants.LOGIN_TOKEN_AUTH);
        if(StringUtils.isEmpty(header) || header.length() < 7){
            throw  new CustomException(ExceptionCode.UNAUTH);
        }
        String jwt = header.substring(7);
        System.out.println(jwt+"/*/*/*");
        AccToken token = null;
        switch (sysType){
            case ADMIN:
                token = JwtTokenUtils.parseToken(jwt);
                break;
            case PORTAL:
                token = JwtTokenUtils.parseWebToken(jwt);
                break;
        }
        System.out.println(token+"********");
        return token;
    }


}
